package com.example.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.entity.Articles;
import com.example.entity.Comment;
import com.example.entity.Comments;
import com.example.mapper.ArticlesMapper;
import com.example.mapper.CommentMapper;
import com.example.mapper.CommentsMapper;
import com.example.mapper.UnionMapper;
import com.example.utils.ResultData;
import com.example.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * The type Comments service.
 */
@Service
public class CommentsService {

    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private ArticlesMapper articlesMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UnionMapper unionMapper;

    /**
     * Add conments result data.
     *
     * @param comments the comments
     * @return the result data
     */
    public ResultData addConments(Comments comments) {
        if (!String.valueOf(comments.getUserName()).isEmpty() && !String.valueOf(comments.getArticleId()).isEmpty()) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            comments.setCommentDate(sdf.format(new Date()));
            int res = commentsMapper.insert(comments);
            if (res > 0) {
                return ResultUtils.sucuss("评论成功", res);
            } else {
                return ResultUtils.error("评论失败。请重试", null);
            }
        } else {
            return ResultUtils.error("请先登录", null);
        }
    }

    /**
     * Gets comments.
     *
     * @param comments the comments
     * @return the comments
     */
    public ResultData getComments(Comments comments) {

        List<Comments> res= unionMapper.getUserComments(comments);

        UpdateWrapper<Articles> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("article_id", comments.getArticleId());
        updateWrapper.set("article_comment_count", res.size());
        articlesMapper.update(null, updateWrapper);
        if (!res.isEmpty()) {
            return ResultUtils.sucuss("查询成功", res);
        } else {
            return ResultUtils.error("暂无评论", null);
        }
    }


    /**
     * Add conment result data.
     *
     * @param comment the comment
     * @return the result data
     */
    public ResultData addConment(Comment comment) {
        if (!String.valueOf(comment.getUserName()).isEmpty() && !String.valueOf(comment.getArticleId()).isEmpty()) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            comment.setCommentDate(sdf.format(new Date()));
            int res = commentMapper.insert(comment);
            if (res > 0) {
                return ResultUtils.sucuss("评论成功", res);
            } else {
                return ResultUtils.error("评论失败。请重试", null);
            }
        } else {
            return ResultUtils.error("请先登录", null);
        }
    }
}
